//
//  UIViewController+AutoAdjust.m
//  WebPDA
//
//  Created by Justin Yip on 8/26/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "UIViewController+AutoAdjust.h"


@implementation UIViewController (AutoAdjust)

/*
 自动根据设备类型，加载相应的nib文件
 */
/*
- (id)init{
	NSString *device = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? @"ipad" : @"iphone";
	NSString *nibName1 = [NSString stringWithFormat:@"%@~%@", [self class], device];
	NSString *nibName2 = [NSString stringWithFormat:@"%@-%@", [self class], device];
	
	NSArray *nibNameArray = [[NSArray alloc] initWithObjects:nibName1, nibName2, NSStringFromClass([self class]), nil];
	BOOL nibFound = NO;
	for (NSString *nibName in nibNameArray) {
		//check if the nib exists
		NSString *nibPath = [[NSBundle mainBundle] pathForResource:nibName ofType:@"nib"];
		if (nibPath) {
			NSLog(@"load nib %@", nibName);
			self = [self initWithNibName:nibName bundle:nil];
			nibFound = YES;
			break;
		}
	}
	if (nibFound) {
		[nibNameArray release];
		return self;
	}
	
	if ((self = [super init])) {
		
	}
	return self;
}
*/
- (id)initWithSmartNib {
	NSString *device = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? @"ipad" : @"iphone";
	NSString *nibName1 = [NSString stringWithFormat:@"%@~%@", [self class], device];
	NSString *nibName2 = [NSString stringWithFormat:@"%@-%@", [self class], device];
	
	NSArray *nibNameArray = [[NSArray alloc] initWithObjects:nibName1, nibName2, NSStringFromClass([self class]), nil];
	BOOL nibFound = NO;
	for (NSString *nibName in nibNameArray) {
		//check if the nib exists
		NSString *nibPath = [[NSBundle mainBundle] pathForResource:nibName ofType:@"nib"];
		if (nibPath) {
			NSLog(@"load nib %@", nibName);
			self = [self initWithNibName:nibName bundle:nil];
			nibFound = YES;
			break;
		}
	}
	[nibNameArray release];
	
	if (!nibFound &&(self = [super init])) {
		NSLog(@"Warning. nib not found for %@", [self class]);
	}
	return self;
}

@end
